package com.example.focusanimator;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;

/**
 * Created by eyesee on 2017/4/12.
 */

public class FlyBorder extends View {
    private boolean isTvScreen = false;
    private View mFocusView;
    private View mSelectView;

    public FlyBorder(Context context) {
        super(context,null,0);
        init(context);
    }

    public FlyBorder(Context context, AttributeSet attrs) {
        super(context, attrs,0);
        init(context);
    }

    public FlyBorder(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {

    }
    /**返回布尔值,判断屏幕是否是想要实现此功能的屏幕类型*/
    public boolean isTvScreen(){
        return isTvScreen;
    }

    public void setTvScreen(boolean isTvScreen){
        this.isTvScreen = isTvScreen;
        invalidate();                   //自动清屏或刷新的含义
    }

    /**设置焦点框,判断当前的view是否为获取到焦点的view
     * 如果不是,将当前的view设置为获取到焦点的view
     * 执行平移动画
     * */
    public void setFocusView(View view,float scale){
        if (mFocusView != view){
            mFocusView = view;
            runTranslateAnimation(mFocusView,scale,scale);
        }
    }

    /**判断当前view是否为选择的view
     * 如果不是,将当前view设置为被选择的view
     * 执行平移动画
     * */
    public void setSelectView(View view){
        if (mSelectView != view){
            mSelectView = view;
            runTranslateAnimation(mSelectView);
        }
    }

    /**平移动画
     * 从当前view移动到被选择的view
     * */
    private void runTranslateAnimation(View toview){
        Rect fromRect = findLocationWithView(this);
        Rect toRect = findLocationWithView(toview);             //获取view的矩形位置
        int x = toRect.left - fromRect.left;
        int y = toRect.top - fromRect.top;                      //获取view的坐标

        int deltaX = (toview.getWidth() - this.getWidth()) / 2;
        int deltaY =(toview.getHeight() - this.getHeight())/2;  //两个view之差的均值

        if (isTvScreen){        //如果不是指定的屏幕类型
            x = dip2px(this.getContext(), x + deltaX);
            y = dip2px(this.getContext(),y + deltaY);           //坐标值加上中间点的距离转为px值
        }else {                 //是指定的屏幕类型
            x = x + deltaX;
            y = y + deltaY;                                     //缩放后view的坐标
        }
        flyWhiteBorder(x,y);                                    //执行平移动画
    }

    /**按xy点距离平移*/
    private void flyWhiteBorder(float x,float y){
        animate().translationX(x).translationY(y).setDuration(500)
                .setInterpolator(new DecelerateInterpolator()).start();
    }

    /**平移缩放动画*/
    public void runTranslateAnimation(View toview, float scalex, float scaley) {
        Rect fromRect = findLocationWithView(this);
        Rect toRect = findLocationWithView(toview);                 //获取到view的矩形位置

        int x = toRect.left - fromRect.left;
        int y = toRect.top - fromRect.top;                          //移动后的view坐标

        int deltaX = (toview.getWidth() - this.getWidth()) / 2;
        int deltaY = (toview.getHeight() - this.getHeight()) / 2;   //两个view之差的均值

        if (isTvScreen){        //如果不是指定的屏幕类型
            x = dip2px(this.getContext(),x+deltaX);
            y = dip2px(this.getContext(),y+deltaY);
        }else {                 //是指定的屏幕类型
            x = x +deltaX;
            y = y +deltaY;                                          //缩放后view的坐标值
        }

        float toWidth = toview.getWidth() * scalex;
        float toHeight = toview.getHeight() * scaley;
        int width = (int) toWidth;
        int height = (int) toHeight;                                //按比例缩放后的view宽高

         flyWhiteBorder(width,height,x,y);
    }

    /**以指定宽高平移到指定坐标点的方法*/
    private void flyWhiteBorder(int width, int height, float x, float y) {
        int mWidth = this.getWidth();
        int mHeight = this.getHeight();

        float scaleX = (float)width / (float) mWidth;
        float scaleY = (float) height / (float)mHeight;             //缩放比例

        animate().translationX(x).translationY(y).setDuration(500)  //执行平移动画及动画时间
                .scaleX(scaleX).scaleY(scaleY).
                setInterpolator(new DecelerateInterpolator()).start();
    }

    /**获取view矩形区域的方法*/
    public Rect findLocationWithView(View view){
        ViewGroup root = (ViewGroup) this.getParent();
        Rect rect = new Rect();
        root.offsetDescendantRectToMyCoords(view,rect);
        return rect;
    }


    private int px2dip(Context context, int pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}

